<template>
  <div class="register-box">
    <div class="logo-box">
      <img class="create-logo" @click="home" src="https://www.ketangpai.cn/images/common/logo_blue.png" alt="logo" />
    </div>
    <div class="register-content">
      <div class="create-left">
        <img src="https://www.ketangpai.cn/images/login/bg.png" alt="背景" style="display: block; height: 538px;" />
      </div>
      <div class="create-right">
        <div class="create-right-content">
          <h2 class="register-title">注册账号</h2>
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="ruleFormRef"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item prop="phone">
              <el-tooltip
                class="item"
                effect="white"
                content="11位电话号码或邮箱"
                placement="left"
              >
                <el-input
                  v-model="ruleForm.phone"
                  autocomplete="off"
                  placeholder="请输入邮箱/手机号"
                  class="input-width"
                ></el-input>
              </el-tooltip>
            </el-form-item>
            <el-form-item prop="account">
                <el-input 
                placeholder="请输入账号" 
                v-model="ruleForm.account"
                class="input-width"
                ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                type="password"
                v-model="ruleForm.password"
                autocomplete="off"
                placeholder="请输入密码"
                class="input-width"
              ></el-input>
            </el-form-item>
            <el-form-item prop="checkPass">
              <el-input
                type="password"
                v-model="ruleForm.checkPass"
                autocomplete="off"
                placeholder="请再次输入密码确认"
                class="input-width"
              ></el-input>
            </el-form-item>
            <p class="text-left">选择身份</p>
            <el-form-item>
              <div class="role-box">
                <div
                  class="item flex-align"
                  :class="{ active: identity === '2' }"
                  @click="selectIdentity('2')"
                >
                  <img src="https://www.ketangpai.cn/img/teacher.b1111283.svg" class="icon" />
                  <span class="name">老师</span>
                </div>
                <div
                  class="item flex-align"
                  :class="{ active: identity === '1' }"
                  @click="selectIdentity('1')"
                >
                  <img src="https://www.ketangpai.cn/img/student.0ba0711e.svg" class="icon" />
                  <span class="name">学生</span>
                </div>
              </div>
            </el-form-item>
            <el-form-item prop="name">
              <el-input
                v-model="ruleForm.name"
                autocomplete="off"
                placeholder="请输入姓名"
                class="input-width"
              ></el-input>
            </el-form-item>
            <el-form-item prop="school">
              <el-input
                v-model="ruleForm.school"
                autocomplete="off"
                placeholder="请输入学校/机构"
                class="input-width"
              ></el-input>
            </el-form-item>

            <el-form-item prop="studentId" v-if="identity === '1'">
              <el-input
                v-model="ruleForm.no"
                autocomplete="off"
                placeholder="请输入学号"
                class="input-width"
              ></el-input>
            </el-form-item>
          </el-form>
          <div class="button-box">
            <div class="magin-top">
              <el-button type="primary" @click="submitForm">注册</el-button>
            </div>
            <div class="magin-top text-right" style="font-size: 14px">
              已有账号？<span @click="pushLogin" style="color: #4285f4; cursor: pointer">去登录</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { post } from '../../utils/request';
// import { getCurrentInstance } from 'vue';


export default {
  name: 'CreateUser',
  setup() {
    const ruleFormRef = ref(null);
    // const { proxy } = getCurrentInstance();

    const ruleForm = ref({
      phone: '',
      account:'',
      password: '',
      checkPass: '',
      name: '',
      school: '',
      no: ''
    });

    const checkRegister = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('格式不正确'));
      }
      if (value.length !== 11 && !value.includes('@')) {
        return callback(new Error('格式不正确'));
      } else {
        callback();
      }
    };

    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (ruleForm.value.checkPass !== '') {
          ruleFormRef.value.validateField('checkPass', () => null);
        }
        callback();
      }
    };

    const validatePassAgain = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== ruleForm.value.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };

    const validateName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入姓名'));
      } else {
        callback();
      }
    };

    const validatePoint = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请选择学校'));
      } else {
        callback();
      }
    };

    const validateStudentId = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入学号'));
      } else {
        callback();
      }
    };

    const rules = ref({
      phone: [
        { validator: checkRegister, trigger: 'blur' }
      ],
      password: [
        { validator: validatePass, trigger: 'blur' }
      ],
      checkPass: [
        { validator: validatePassAgain, trigger: 'blur' }
      ],
      name: [
        { validator: validateName, trigger: 'blur' }
      ],
      school: [
        { validator: validatePoint, trigger: 'blur' }
        ],
      account: [
        { required: true, message: '请输入账号', trigger: 'blur' }
      ],
      no: [
        { validator: validateStudentId, trigger: 'blur' }
      ]
    });

    const identity = ref('2');
    const router = useRouter();

    const submitForm = async () => {
      const valid = await ruleFormRef.value.validate();
      if (valid) {
        try {
          const formData = {
          ...ruleForm.value,
          type: identity.value
        };
        const res =await post('/user/register', formData);
        if (res) {
            console.log(res)
            router.push('/login');
        }
        } catch (e) {
          console.log(e);
        }
        
      } else {
        console.log('error submit!!');
        return false;
      }
    };

    const resetForm = () => {
      ruleFormRef.value.resetFields();
    };

    const pushLogin = () => {
      router.push('/login');
    };

    const selectIdentity = (role) => {
      identity.value = role;
    };

    const home = () => {
      router.push('/');
    };

    return {
      ruleFormRef,
      ruleForm,
      rules,
      identity,
      submitForm,
      resetForm,
      pushLogin,
      selectIdentity,
      home
    };
  }
};
</script>

<style scoped>
.role-box .item.active {
  border: 1px solid blue;
}

.register-box {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 100vh;
  padding-top: 20px;
  background: #f8f9fa;
  background-repeat: no-repeat;
  background-size: 100%;
}

.logo-box {
  position: absolute;
  top: 48px;
  left: 62px;
  cursor: pointer;
}

.logo-box img {
  display: block;
  max-width: 136px;
  max-height: 34px;
}

.register-content {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: space-between;
}

.create-left {
  margin-right: -100px;
}

.create-left {
  display: block;
  height: 538px;
}

.logo-box {
  display: block;
  max-width: 136px;
  max-height: 34px;
}

.register-content .create-right {
  margin: 12px 0;
}

.create-right-content {
  width: 431px;
  padding: 0 35px;
  background: #fff;
  border: 1px solid #eee;
  box-shadow: 0 0 61px 0 rgba(85, 108, 144, 0.07);
}

.create-right .register-title {
  position: relative;
  text-align: center;
}

.button-box {
  margin-top: 12px;
  padding-bottom: 12px;
}

.magin-top {
  margin-top: 12px;
}

.text-right {
  text-align: right;
}

.text-left {
  text-align: left;
}

.role-box {
  display: flex;
  justify-content: space-between;
}

.role-box .item {
  width: 172px;
  padding: 8px 16px;
  border: 1px solid #fff;
}

.flex-align {
  display: flex;
  align-items: center;
}

.role-box .active {
  border: 1px solid #4285f4;
}

.item .icon {
  display: block;
  width: 42px;
  height: 42px;
  margin-right: 9px;
  border-radius: 50%;
}

.item .name {
  color: #3c4043;
  font-size: 14px;
  line-height: 24px;
  letter-spacing: 1px;
  text-align: center;
}
:deep(.el-form-item__content) {
  margin-left: 0 !important;
}

.button-box button {
  width: 100%;
  color: #fff;
  background-color: #4285f4;
  border-color: #4285f4;
}
</style>
